package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.entity.Schedule;
import com.guotd.ticketSystem.mapper.ScheduleMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getSchedules")
public class GetSchedulesServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = SessionUtil.getSqlSession();
        ScheduleMapper scheduleMapper = session.getMapper(ScheduleMapper.class);
        List<Schedule> schedules = scheduleMapper.getSchedules();
        if (!schedules.isEmpty()) {
            request.getSession().setAttribute("schedules", schedules);
            request.getRequestDispatcher("schedule_list.jsp").forward(request, response);
        }
        else {
            request.getRequestDispatcher("schedule_list.jsp").forward(request, response);
        }
    }
}
